define(['jquery','mui','template'], function ($,$$,Template) {
    var Page = {

        APIURL: '',
        config: {
            pageSetting: {
                sort: Fast.api.query('sort')? Fast.api.query('sort'):'id',
                order: Fast.api.query('order')? Fast.api.query('order'):'desc',
                offset: Fast.api.query('offset')? Fast.api.query('offset'):'0',
                limit: Fast.api.query('limit')? Fast.api.query('limit'):10,
                filter: Fast.api.query('filter')? Fast.api.query('filter'):JSON.stringify({}),
                op: Fast.api.query('op')? Fast.api.query('op'):JSON.stringify({}),
            },
            url: '',
            dom:'#list',
            template_tpl: 'list-tpl',
            more_dom: '#more',
            more_template_tpl: 'more-tpl',
            searchfield: 'nickname',
            is_pullrefresh: 1,
            refreshContainer: "#refreshContainer",
            pickerfield: 'hotel_id'
        },
        filter: {},
        op:{},
        totalpage:1,
        currentpage:0,
        hotelPicker: null,
        api: {
            init:function(params){

                params = params ? params : {};
                $.extend(true, Page.config, params);

            },
            getData:function() {

                Page.config.pageSetting.filter = JSON.stringify(Page.filter);
                Page.config.pageSetting.op = JSON.stringify(Page.op);
                // Page.config.pageSetting.offset = Page.currentpage * Page.config.pageSetting.limit;

                // console.log(Fast.api.fixurl(Page.config.url))
                $.ajax({
                    type: Page.config.type ? Page.config.type : 'GET',
                    url: Fast.api.fixurl(Page.config.url),
                    // data: Page.config.pageSetting,
                    // data:'{"id":"1","pwd":"123"}',
                    data: Page.config.pageSetting,
                    dataType: 'json',
                    success: function(ret){
                        console.log(ret)
                        var dom = $(Page.config.dom);

                        var template_tpl = Page.config.template_tpl;
                        Page.currentpage = ret.currentpage;
                        Page.totalpage = ret.totalpage;
                        $("#total").html(ret.total)
                        if(ret.total > 0)
                        {

                            if(ret.currentpage == 1)
                                dom.html(Template(template_tpl, ret));
                            else
                                dom.append(Template(template_tpl, ret));

                            if(ret.currentpage < ret.totalpage)
                                msg = '加载更多';
                            else
                                msg = '加载完毕';
                        }else{

                            msg = '暂无数据';
                            dom.html('');
                        }

                        if(ret.currentpage == ret.totalpage)
                        {

                            // $$("#refreshContainer").pullRefresh()..endPulldownToRefresh();();
                        }
                        if(Page.config.is_pullrefresh == 1)
                        {

                            $$(refreshContainer).pullRefresh().endPulldownToRefresh();
                            // console.log(ret.rows.length)
                            if(ret.total > 0 && ret.totalpage > 0)
                            {
                                $$(refreshContainer).pullRefresh().endPullupToRefresh(ret.currentpage >= ret.totalpage);

                            } else {
                                $$(refreshContainer).pullRefresh().endPullupToRefresh(true);

                            }
                            if(ret.rows.length < 2){//上拉加载数据，数据为空时，隐藏“正在刷新”内容
                                $('#refreshContainer').find('.mui-pull-top-pocket').removeClass('mui-visibility');
                            }

                            // if(ret.total == 0) {
                            //     console.log("aaaa")
                            // }else {
                            //     console.log("bbbbb")
                            //     $$(refreshContainer).pullRefresh().endPullupToRefresh((Page.currentpage >= Page.totalpage));
                            // }
                            // if(Page.currentpage == Page.totalpage) {

                            // }

                        }



                        // var more_dom = $(Page.config.more_dom);
                        // var more_template_tpl = Page.config.more_template_tpl;
                        // // console.log(Template(more_template_tpl, {currentpage: ret.currentpage,totalpage:ret.totalpage,msg: msg}));
                        // more_dom.html(Template(more_template_tpl, {currentpage: ret.currentpage,totalpage:ret.totalpage,msg: msg}));
                        // console.log(Template(more_template_tpl, {currentpage: ret.currentpage,totalpage:ret.totalpage,msg: msg}));
                    },
                    error: function(xhr, type){
                      // alert('Ajax error!')
                    // callback(xhr)
                    }
                  })
            },
            inputSearchKeyup: function() {
                // 点击搜索按钮
                $('#search-btn').bind('tap', function () {
                    var value = $('#search').val()
                    Page.api.searchEvent(value);
                });
                // 点击取消按钮
                $('#cancel-btn').bind('tap', function () {
                    Page.api.searchEvent('');
                })
                $('#search').bind('keyup', function (event) {
                    var event = window.event || arguments.callee.caller.arguments[0];
                    var value = $(this).val()
                    if(value == ''&& value.length<1){
                        $('.mui-icon-clear').addClass('mui-hidden');
                    }else{
                        $('.mui-icon-clear').removeClass('mui-hidden');
                    }
                    if(event.keyCode == 13 || !value){
                        Page.api.searchEvent(value);
                    }
                });
            },
            searchEvent: function (value) {
                var field = Page.config.searchfield;
                // if (value) {
                    $$('#refreshContainer').pullRefresh().refresh(true);
                    Page.config.pageSetting.offset = 0;
                    Page.op[field] = 'like';
                    Page.filter[field] = value;
                    Page.api.getData();
                // }
            },
            btnSearch: function() {
                var field = Page.config.searchfield;
                $('#search').blur(function (){
                    var value = $(this).val()
                    console.log(value);
                    Page.config.pageSetting.offset = 0;
                    Page.op[field] = 'like';
                    Page.filter[field] = value;
                    // Page.api.pullRefresh();
                    Page.api.getData();
                });
            },
            btnPicker: function() {
                $(document).on("click", ".screenhotelpicker", function() {

                    $$('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);
                    var field = Page.config.pickerfield;
                    Page.hotelPicker.show(function(items) {
                        var value = items[0].value;
                        Page.config.pageSetting.offset = 0;
                        Page.op[field] = '=';
                        Page.filter[field] = value;
                        Page.api.getData();
                    })
                })
            },
            cancelSearch: function() {
                $("#searchCancel").click(function(){
                    Page.op = {};
                    Page.filter = {};
                    Page.config.pageSetting.offset = 0;
                    Page.api.getData();
                })
            },
            loadMore: function(){
                $('.weui-loadmore').on('click', '.weui-loadmore__tips', function() {
                    var page = $(this).data('page')
                    Page.config.pageSetting.offset = page * Page.config.pageSetting.limit;
                    Page.api.getData();
                })
            },
            pullRefresh: function() {

                $$("#refreshContainer").pullRefresh({
                    up:{
                        contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
                        auto: true,
                        contentnomore:'没 有 更 多 数 据 了',//可选，请求完毕若没有更多数据时显示的提醒内容；
                        callback:function(){//必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；

                            if(Page.currentpage < Page.totalpage){
                                window.setTimeout(function(){
                                    var page = Page.currentpage
                                    Page.config.pageSetting.offset = page * Page.config.pageSetting.limit;
                                    Page.api.getData();
                                },500);
                            }
                        }
                    },
                    down : {
                        height:50,//可选,默认50.触发下拉刷新拖动距离,
                        auto: false,//可选,默认false.首次加载自动下拉刷新一次
                        contentdown : "下拉可以刷新",//可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
                        contentover : "释放立即刷新",//可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
                        contentrefresh : "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
                        callback :function(){ //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
                            // $$('#refreshContainer').pullRefresh().endPullupToRefresh(true)
                            $$('#refreshContainer').pullRefresh().refresh(true);
                            Page.config.pageSetting.offset = 0;
                            Page.api.getData();

                        }
                    }
                })


            },
            cssinit: function (){
                console.log($("#search").length);
                if($("#search").length == 0){
                    $("#refreshContainer .mui-scroll").css('padding-top','44px');
                }else
                    $("#refreshContainer .mui-scroll").css('padding-top','90px');
            },
            bindevent: function(params) {

                Page.api.cssinit();
                Page.api.init(params);
                Page.api.btnSearch();
                Page.api.cancelSearch();
                Page.api.btnPicker();
                Page.api.loadMore();
                Page.api.inputSearchKeyup();
                if(Page.config.is_pullrefresh)
                    Page.api.pullRefresh();
                else
                    Page.api.getData();
            }
        }
    }
    return Page;
})